home *** CD-ROM | disk | FTP | other *** search
/ isnet Internet / Isnet Internet CD.iso / prog / hiz / 09 / 09.exe / adynware.exe / perl / lib / Net / protoent.pm < prev    next >
Encoding:
Perl POD Document  |  1999-12-28  |  3.0 KB  |  94 lines

  1. package Net::protoent;
  2. use strict;
  3.  
  4. BEGIN { 
  5.     use Exporter   ();
  6.     use vars       qw(@EXPORT @EXPORT_OK %EXPORT_TAGS);
  7.     @EXPORT      = qw(getprotobyname getprotobynumber getprotoent);
  8.     @EXPORT_OK   = qw( $p_name @p_aliases $p_proto );
  9.     %EXPORT_TAGS = ( FIELDS => [ @EXPORT_OK, @EXPORT ] );
  10. }
  11. use vars      @EXPORT_OK;
  12.  
  13. sub import { goto &Exporter::import }
  14.  
  15. use Class::Struct qw(struct);
  16. struct 'Net::protoent' => [
  17.    name        => '$',
  18.    aliases    => '@',
  19.    proto    => '$',
  20. ];
  21.  
  22. sub populate (@) {
  23.     return unless @_;
  24.     my $pob = new();
  25.     $p_name      =    $pob->[0]              = $_[0];
  26.     @p_aliases     = @{ $pob->[1] } = split ' ', $_[1];
  27.     $p_proto     =    $pob->[2]          = $_[2];
  28.     return $pob;
  29.  
  30. sub getprotoent      ( )  { populate(CORE::getprotoent()) } 
  31. sub getprotobyname   ($)  { populate(CORE::getprotobyname(shift)) } 
  32. sub getprotobynumber ($)  { populate(CORE::getprotobynumber(shift)) } 
  33.  
  34. sub getproto ($;$) {
  35.     no strict 'refs';
  36.     return &{'getprotoby' . ($_[0]=~/^\d+$/ ? 'number' : 'name')}(@_);
  37. }
  38.  
  39. 1;
  40.  
  41. __END__
  42.  
  43. =head1 NAME
  44.  
  45. Net::protoent - by-name interface to Perl's built-in getproto*() functions
  46.  
  47. =head1 SYNOPSIS
  48.  
  49.  use Net::protoent;
  50.  $p = getprotobyname(shift || 'tcp') || die "no proto";
  51.  printf "proto for %s is %d, aliases are %s\n",
  52.     $p->name, $p->proto, "@{$p->aliases}";
  53.  
  54.  use Net::protoent qw(:FIELDS);
  55.  getprotobyname(shift || 'tcp') || die "no proto";
  56.  print "proto for $p_name is $p_proto, aliases are @p_aliases\n";
  57.  
  58. =head1 DESCRIPTION
  59.  
  60. This module's default exports override the core getprotoent(),
  61. getprotobyname(), and getnetbyport() functions, replacing them with
  62. versions that return "Net::protoent" objects.  They take default
  63. second arguments of "tcp".  This object has methods that return the
  64. similarly named structure field name from the C's protoent structure
  65. from F<netdb.h>; namely name, aliases, and proto.  The aliases method
  66. returns an array reference, the rest scalars.
  67.  
  68. You may also import all the structure fields directly into your namespace
  69. as regular variables using the :FIELDS import tag.  (Note that this still
  70. overrides your core functions.)  Access these fields as variables named
  71. with a preceding C<p_>.  Thus, C<$proto_obj-E<gt>name()> corresponds to
  72. $p_name if you import the fields.  Array references are available as
  73. regular array variables, so for example C<@{ $proto_obj-E<gt>aliases()
  74. }> would be simply @p_aliases.
  75.  
  76. The getproto() function is a simple front-end that forwards a numeric
  77. argument to getprotobyport(), and the rest to getprotobyname().
  78.  
  79. To access this functionality without the core overrides,
  80. pass the C<use> an empty import list, and then access
  81. function functions with their full qualified names.
  82. On the other hand, the built-ins are still available
  83. via the C<CORE::> pseudo-package.
  84.  
  85. =head1 NOTE
  86.  
  87. While this class is currently implemented using the Class::Struct
  88. module to build a struct-like class, you shouldn't rely upon this.
  89.  
  90. =head1 AUTHOR
  91.  
  92. Tom Christiansen
  93.